﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IdentityAtRest.Utility.Identity;
using IdentityAtRest.Utility.Identity.Authorization;
using SimpleExpressionEvaluator;
using SimpleExpressionEvaluator.Parsers.Irony;

namespace IdentityAtRest.Service.Authorization
{
    public class ExpressionAuthorizationManager : AuthorizationManager
    {
        public ExpressionAuthorizationManager()
            : base(new PermissionPolicyRepository(
                new SimpleExpressionEval()))
        {
        }

        public ExpressionAuthorizationManager(IExpressionEvaluator eval)
            :base(new PermissionPolicyRepository(eval))
        {
        }

        public IAuthorizationDecision AuthorizePolicyExpression(IList<IClaim> userClaims,string policyExpression)
        {
            IPermissionPolicy dynamicPolicy =
                ((PermissionPolicyRepository) base.PermissionPolicyRepository)
                    .CreatePolicy("dynamic", policyExpression);

            return dynamicPolicy.Authorize(userClaims);
        }
    }
}
